package org.sith.gl.taskmanager.client.tasks;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;
import org.sith.gl.taskmanager.client.TasksRemoteService;
import org.sith.gl.taskmanager.client.beans.TaskEntityGWT;


public class TaskPanel extends VerticalPanel {
    private TaskEntityGWT taskEntityGWT;

    public TaskPanel(TaskEntityGWT taskEntityGWT) {
        this.taskEntityGWT = taskEntityGWT;
    }

    public void createWidget() {
        setStyleName("openTask");
        setWidth("100%");

        HorizontalPanel infoPanel = createTaskInfoPanel();
        add(infoPanel);

        add(new Label("Task description: " + taskEntityGWT.getDescription()));
        if (taskEntityGWT.isComplete()) {
            infoPanel.add(new Label("Close description: " + taskEntityGWT.getCloseDescription()));
        }

        if (!taskEntityGWT.isComplete()) {
            Button closeTaskButton = new Button("Close task");
            closeTaskButton.addClickHandler(new CloseTaskHandler());
            add(closeTaskButton);
        } else {

            add(new Label("Task closed"));
            setStyleName("closedTask");
        }


    }

    private HorizontalPanel createTaskInfoPanel() {
        HorizontalPanel hp = new HorizontalPanel();
        VerticalPanel left = createLeftPanel();
        VerticalPanel right = createRightPanel();
        hp.setCellHorizontalAlignment(left, HasHorizontalAlignment.ALIGN_RIGHT);
        hp.setCellHorizontalAlignment(right, HasHorizontalAlignment.ALIGN_CENTER);
        hp.add(left);
        hp.add(right);
        hp.setWidth("100%");
        return hp;
    }

    private VerticalPanel createRightPanel() {
        VerticalPanel right = new VerticalPanel();
        right.add(new Label("Creation Date: " + taskEntityGWT.getCreatedDate()));
        right.add(new Label("Due Date: " + taskEntityGWT.getDueDate()));
        return right;
    }


    private VerticalPanel createLeftPanel() {
        VerticalPanel left = new VerticalPanel();
        left.add(new Label("Added By: " + taskEntityGWT.getAddedBy().getLogin()));
        left.add(new Label("Assigned To: " + taskEntityGWT.getAssignedTo().getLogin()));
        return left;
    }

    private class CloseTaskHandler implements ClickHandler {
        @Override
        public void onClick(ClickEvent clickEvent) {
            final PopupPanel closedDescriptionPanel = new PopupPanel();
            closedDescriptionPanel.setPopupPosition(200,200);
            VerticalPanel vp = new VerticalPanel();
            closedDescriptionPanel.add(vp);
            final TextArea closeDesc = new TextArea();
            vp.add(closeDesc);
            HorizontalPanel buttonsPanel = new HorizontalPanel();
            Button ok = new Button("Ok");

            ok.addClickHandler(new PopupOkButtonHandler(closeDesc, closedDescriptionPanel));
            buttonsPanel.add(ok);
            Button cancel = new Button("Cancel");
            buttonsPanel.add(cancel);
            vp.add(buttonsPanel);
            closedDescriptionPanel.show();
        }

        private class PopupOkButtonHandler implements ClickHandler {
            private final TextArea closeDesc;
            private final PopupPanel closedDescriptionPanel;

            public PopupOkButtonHandler(TextArea closeDesc, PopupPanel closedDescriptionPanel) {
                this.closeDesc = closeDesc;
                this.closedDescriptionPanel = closedDescriptionPanel;
            }

            @Override
            public void onClick(ClickEvent clickEvent) {
                String text = closeDesc.getText();
                if (text==null||text.isEmpty()) {
                    Window.alert("Please type close description");
                    return;
                }
                taskEntityGWT.setCloseDescription(text);
                taskEntityGWT.setComplete(true);
                updateTask();
            }

            private void updateTask() {
                TasksRemoteService.App.getInstance().saveOrUpdateTask(taskEntityGWT, new AsyncCallback<Void>() {

                    @Override
                    public void onFailure(Throwable throwable) {
                        closedDescriptionPanel.hide();
                        Window.alert("Can't close task: " + throwable.getMessage());
                    }

                    @Override
                    public void onSuccess(Void aVoid) {
                        closedDescriptionPanel.hide();
                        TaskPanel.this.removeFromParent();
                    }
                });
            }
        }
    }
}
